﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufBase.h"
#include <QtWidgets/QLabel>

namespace gnuf {
    class QtHeader;
    class QtPalletButton;
    class Pallet;
    class Header;
    class Info;

    class QtHeaderLabel final : public QLabel
    {
    public:
        explicit QtHeaderLabel(QtHeader *, bool asIcon);
        virtual ~QtHeaderLabel() {}
        friend class QtHeader;
    private:
        void setStyle();

        QtHeader * m_qtHeader;
        gcmp::OwnerPtr< Info > m_info;
        /*
            Header:
            +------+----------------------------------------+--------+--------+
            | Icon |////////////////////////////////////////| Button | Button |
            +------+----------------------------------------+--------+--------+
                   .                                        .
                   +----------------------------------------+
           Case 1  | Caption                                |
                   +----------------------------------------+
                   .                                        .
                   +----------------------------------------+
           Case 2  | Icon-Tab \ Icon-Tab \                  |
                   +----------------------------------------+
                   .                                        .
                   +----------------------------------------+
           Case 3  | Caption-Tab \ Caption-Tab \            |
                   +----------------------------------------+
        */
        enum EStyle : std::uint8_t
        {
            eIcon       = 1 << 0,
            eCaption    = 1 << 1
        };
        const std::uint8_t m_style;
        gcmp::OwnerPtr< QPixmap > m_normal, m_hovered, m_selected;
    };

    class QtHeaderLabelUnderline final : public QLabel
    {
    public:
        explicit QtHeaderLabelUnderline(QtHeader *);
        virtual ~QtHeaderLabelUnderline() {}

        void setStyle();
    protected:
        // Inherits from QLabel...
        virtual void showEvent(QShowEvent *) override;
    private:
        QtHeader * m_qtHeader;
    };
    
    class GNUF_BASE_EXPORT QtHeader : public QWidget
    {
        Q_OBJECT
    public:
        explicit QtHeader(Header *);
        explicit QtHeader(gcmp::OwnerPtr<Header> &&);
        virtual ~QtHeader();

        inline const Header * header() const;
        inline Header * header();

        void setCurrentQtTab(QtPalletButton*);
        inline QtPalletButton* currentQtTab() const { return m_currentQtTab; }
        int indexOf(const QtPalletButton *) const;
        int currentIndex() const;

        virtual void refresh();
        void click(QtPalletButton*);

    signals:
        void onTabChanged(int currentTabIndex);

    protected:
        // Inherits from QWidget...
        virtual void paintEvent(QPaintEvent *) override;

        void assignHeight(int);
    private:
        void initialize();
        void setStyle();
        
        gcmp::OwnerPtr<Header> m_opHeader;
        gcmp::WeakPtr<Header> m_wpHeader;
        int m_height;
        gcmp::OwnerPtr< QtHeaderLabel > m_qtIcon;
        gcmp::OwnerPtr< QtHeaderLabel > m_qtCaption;
        QtPalletButton* m_currentQtTab;
        std::list< gcmp::OwnerPtr< QtPalletButton > > m_qtTabs;
        gcmp::OwnerPtr< QtHeaderLabelUnderline > m_qtUnderline;
    };

    class QtPalletHeader final : public QtHeader
    {
        Q_OBJECT
    public:
        QtPalletHeader(Pallet *);
        virtual ~QtPalletHeader();

        Pallet * pallet() { return m_wpPallet.Get(); }
        const Pallet * pallet() const { return m_wpPallet.Get(); }

        int indexOf(const QtPalletButton *) const;
        void onButtonClicked(QtPalletButton*);
        virtual void refresh() override;

    protected:
        // Inherits from QWidget...
        virtual void mousePressEvent(QMouseEvent *) override;
        virtual void mouseReleaseEvent(QMouseEvent *) override;
        virtual void mouseMoveEvent(QMouseEvent *) override;
        virtual void mouseDoubleClickEvent(QMouseEvent *) override;
        virtual void resizeEvent(QResizeEvent *) override;
    private slots:
        void onHandleTabChanged(int);
    private:
        gcmp::WeakPtr<Pallet> m_wpPallet;
        std::list< gcmp::OwnerPtr< QtPalletButton > > m_qtButtons;
        QPoint m_globalPos;
    };

}
